package com.youli.gb.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.youli.gb.model.SysRole;
import com.youli.gb.service.ISysRoleService;
import com.youli.gb.service.impl.SysRoleServiceImpl;

@WebServlet("/role.do")
public class SysRoleServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;	
	
	private ISysRoleService roleService = new SysRoleServiceImpl();	
       
    public SysRoleServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String type = request.getParameter("type");
		String roleName = request.getParameter("roleName");
		String description = request.getParameter("description");
		
		
		if (type.equals("list")) {
			list(request, response);
		}else if(type.equals("add")) {
			add(request, response, roleName, description);
		}else if(type.equals("delete")) {
			delete(request, response);
		}else if(type.equals("deleteByIds")) {
			deleteByIds(request, response);
		}else if(type.equals("get")) {
			get(request, response);
		}else {
			Integer id = Integer.valueOf(request.getParameter("id"));
			int ret = this.roleService.edit(id,description);
			if(ret > 0) {
				request.setAttribute("msg", "修改成功！");
			}else {
				request.setAttribute("msg", "修改失败！");				
			}
			request.getRequestDispatcher("role/roleresult.jsp").forward(request, response);			
		}
	}

	private void get(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer id = Integer.valueOf(request.getParameter("id"));
		SysRole role = this.roleService.getById(id);
		request.setAttribute("role", role);
		request.getRequestDispatcher("role/editrole.jsp").forward(request, response);
	}

	private void deleteByIds(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String ids = request.getParameter("ids");//1,2,3,4
		int ret = this.roleService.deleteByIds(ids.split(","));
		if(ret > 0) {
			request.setAttribute("msg", "删除成功！");
		}else {
			request.setAttribute("msg", "删除失败！");				
		}
		request.getRequestDispatcher("role/roleresult.jsp").forward(request, response);
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer id = Integer.valueOf(request.getParameter("id"));
		int ret = this.roleService.deleteById(id);
		if(ret > 0) {
			request.setAttribute("msg", "删除成功！");
		}else if(ret == 0){
			request.setAttribute("msg", "删除失败！");				
		}else {
			request.setAttribute("msg", "待删除角色存在关联数据！");	
		}
		request.getRequestDispatcher("role/roleresult.jsp").forward(request, response);
	}

	private void add(HttpServletRequest request, HttpServletResponse response, String roleName, String description)
			throws ServletException, IOException {
		if(StringUtils.isNotBlank(roleName)) {
			SysRole role = this.roleService.getByRoleName(roleName);
			if(role == null) {
				int ret = this.roleService.add(roleName,description);
				if(ret > 0) {
					request.setAttribute("msg", "添加成功！");
				}else {
					request.setAttribute("msg", "添加失败！");				
				}
				request.getRequestDispatcher("role/roleresult.jsp").forward(request, response);
			}else {
				request.setAttribute("msg", "已经存在同名角色，请更换！");
				request.getRequestDispatcher("role/addrole.jsp").forward(request, response);
			}			
		}else {			
			request.setAttribute("msg", "角色名称不能为空！");
			request.getRequestDispatcher("role/addrole.jsp").forward(request, response);
		}
	}

	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//从service中获取所有角色信息
		List<SysRole> roles = roleService.getAll();
		request.setAttribute("roles", roles);
		request.getRequestDispatcher("role/roles.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}




















